<template>
  <div class="tools-div">
    <el-button type="success" size="small" @click="addShow">添 加</el-button>
  </div>
  <!--表单弹窗数据-->
  <el-dialog v-model="dialogVisible" :title="dialogTitle" width="30%">
    <el-form label-width="120px">
      <el-form-item label="菜单标题">
        <el-input v-model="sysMenu.title" />
      </el-form-item>
      <el-form-item label="路由名称">
        <el-input v-model="sysMenu.component" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="sysMenu.sortValue" />
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="sysMenu.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate">提交</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
  <!---===========================================================================-->
  <!--表格展示数据-->
  <el-table
    :data="list"
    style="width: 100%; 
    margin-bottom: 20px"
    row-key="id"
    border
    default-expand-all
    stripe="true"
  >
    <el-table-column prop="title" label="菜单标题" />
    <el-table-column prop="component" label="路由名称" />
    <el-table-column prop="sortValue" label="排序" />
    <el-table-column prop="status" label="状态" #default="scope">
      {{ scope.row.status == 1 ? '正常' : '停用' }}
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" />

    <el-table-column label="操作" align="center" width="280" #default="scope">
      <el-button type="success" size="small" @click="addShow(scope.row)">
        添加下级节点
      </el-button>
      <el-button type="primary" size="small" @click="editShow(scope.row)">
        修改
      </el-button>
      <el-button type="danger" size="small" @click="remove(scope.row.id)">
        删除
      </el-button>
    </el-table-column>
  </el-table>
</template>

<script setup>
//引入调用的方法
import { ref, onMounted } from 'vue'
import {
  getMenuList,
  postSaveMenu,
  postUpdateMenu,
  deleteMenu,
} from '@/api/menu'
import { ElMessage, ElMessageBox } from 'element-plus'
// import { ElMessage, ElMessageBox } from 'element-plus'

// 定义表格数据模型
const list = ref([])

// 定义添加表单菜单表单相关数据模型
const dialogTitle = ref('添加菜单')
const dialogVisible = ref(false)

//页面表单数据
const defaultForm = {
  id: '',
  parentId: 0,
  title: '',
  url: '',
  component: '',
  icon: '',
  sortValue: 1,
  status: 1,
}
// 表单响应式数据模型对象
const sysMenu = ref(defaultForm)

//=======================分页列表====================
const fetchData = async () => {
  const { data } = await getMenuList()
  list.value = data
}
//==========================添加==================================================
//打开弹窗
const addShow = row => {
  dialogVisible.value = true
  sysMenu.value = {}
  if (!row.id) {
    dialogTitle.value = '添加一级菜单'
  } else {
    dialogTitle.value = '添加下级节点'
    //添加子级菜单的方法是parentId = 当前节点的菜单id,这里添加的是子级菜单
    sysMenu.value.parentId = row.id
  }
}
//调用后端添加菜单(注意这里添加的是一级菜单)接口
const saveOrUpdate = async () => {
  //判断是否存在菜单id
  if (!sysMenu.value.id) {
    //判断parentId为false设置parentId为0,这个添加的数据是一级菜单
    if (!sysMenu.value.parentId) {
      sysMenu.value.parentId = 0
    }
    const { code } = await postSaveMenu(sysMenu.value)
    if (code === 200) {
      dialogVisible.value = false
      fetchData()
      ElMessage.success('添加菜单成功!')
    }
  } else {
    //调用后端修改菜单接口
    const { code } = await postUpdateMenu(sysMenu.value)
    if (code === 200) {
      dialogVisible.value = false
      ElMessage.success('修改菜单成功!')
      fetchData()
    }
  }
}
//============================修改菜单=========================================
const editShow = row => {
  dialogVisible.value = true
  sysMenu.value = { ...row }
  dialogTitle.value = '修改菜单'
}
//===========================删除菜单=================================================
const remove = async id => {
  ElMessageBox.confirm('是否确认删除', '删除菜单', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code } = await deleteMenu(id)
    if (code === 200) {
      ElMessage.success('删除菜单成功！')
      fetchData()
    }
  })
}

//=======================加载数据=========================
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
</style>
